package com.situ.day3;

import org.junit.Test;

import java.util.Arrays;

public class ArrayDemo {

    @Test
    public void test1() {
        int num = 3;
        int[] array = new int[4];
        System.out.println(array.length);
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;
        System.out.println(array[0]);
        System.out.println(array[1]);
        System.out.println(array[2]);
        System.out.println(array[3]);
        System.out.println("-----------------");
//        for (int i = 0; i <= array.length - 1; i++) {
//            System.out.println(i);
//        }
        for (int i = 0; i < array.length; i++) {
            System.out.println("i: " + i);
            System.out.println(array[i]);
        }
        System.out.println("----------------");
        for (int i = array.length - 1; i >= 0; i--) {
            System.out.println(array[i]);
        }
     }

     @Test
     public void test2() {
        int[] array1 = {2, 45, 56, 78, 9};
        System.out.println(array1.length);
        int[] array2 = new int[]{2, 45, 56, 78, 9};
     }

     @Test
     public void test3() {
         int[] array = new int[4];
         array[0] = 23;
         array[1] = 3;
         array[2] = 4;
         array[3] = 24;
         //fori、array.fori
         //数组下标越界异常   Runtime
         //java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
         for (int i = 0; i <= array.length; i++) {
             System.out.println(array[i]);
         }

     }

    @Test
    public void test4() {
        //int num1, num2;
        int num1;
        int num2;
        int[] array = new int[4];
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        System.out.println("sum: " + sum);

    }

    @Test
    public void test5() {
        int[] array = new int[4];
        array[0] = 23;
        array[1] = 63;
        array[2] = 74;
        array[3] = 24;
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        System.out.println("max: " + max);

        int max1 = getMax(array);
        System.out.println("max1: " + max1);
    }

    public int getMax(int[] array) {
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        return max;
    }

    @Test
    public void test33() {
        int[] array = {40, 17, 21, 1};
        //外层循环控制轮数
        for (int i = 1; i <= array.length - 1; i++) {
            for (int j = 0; j < array.length - i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }

        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

    @Test
    public void test332() {
        int[] array = {40, 17, 21, 1};
        sort(array);

        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

    public void sort(int[] array) {
        for (int i = 1; i <= array.length - 1; i++) {
            for (int j = 0; j < array.length - i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }

    @Test
    public void test11() {
        int num1 = 3;
        int num2 = 5;
        int temp = num2;
        num2 = num1;
        num1 = temp;
        System.out.println(num1);
        System.out.println(num2);
    }

    @Test
    public void test133() {
        int[] source = {41, 2, 63, 4, 5};
        int[] dest = new int[5];
        System.arraycopy(source, 0, dest, 0, source.length);

        for (int i = 0; i < dest.length; i++) {
            System.out.println(dest[i]);
        }
        System.out.println("-------------");
        //增强的for循环
        for (int item : dest) {
            System.out.println(item);
        }
    }

    @Test
    public void test35() {
        int[] source = {1, 2, 3, 4, 5};
        int[] copiedArray1 = Arrays.copyOf(source, source.length);
        for (int i : copiedArray1) {
            System.out.println(i);
        }
        System.out.println("-----------------------");
        int[] copiedArray2 = Arrays.copyOf(source, 10);
        for (int i : copiedArray2) {
            System.out.println(i);
        }
        int[] array1 = new int[10];//0
        boolean[] array2 = new boolean[10];//false
        double[] array3 = new double[10];//0.0
    }

    @Test
    public void test633() {
        int[] source = {1, 2, 3, 4, 5};

        int[] array1 = Arrays.copyOfRange(source, 1, 4); // 复制索引 1 到 3 的元素
        for (int num : array1) {
            System.out.print(num + " ");
        }

        System.out.println();

        int[] dest = new int[3];
        System.arraycopy(source, 1, dest, 0, 3);
        for (int num : dest) {
            System.out.print(num  + " ");
        }
    }
    
    @Test
    public void test99() {
        int[] source = {561, 2, 3, 4, 5};
        int[] array = source.clone();
        for (int num : array) {
            System.out.println(num);
        }
    }

    @Test
    public void test334() {
        int[][] array = new int[3][];
        array[0] = new int[2];
        array[1] = new int[2];
        array[2] = new int[3];
        array[0][0] = 23;
        array[0][1] = 12;
        array[1][0] = 22;
        array[1][1] = 22;
        array[2][0] = 21;
        array[2][1] = 22;
        array[2][2] = 23;

        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.println(array[i][j]);
            }
        }
        System.out.println("-------------");
        for (int[] arr : array) {
            for (int item : arr) {
                System.out.println(item);
            }
        }
    }
}
